package net.mikoo.seals.api.model.use;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.common.enums.CensorStatusEnum;
import net.mikoo.seals.common.enums.UsedStatusEnum;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.model.DevicesUsed;

public class UseRecordResponse extends BaseResponse {

	private static final long serialVersionUID = -1067659708998977468L;

	private List<UseInfo> useInfos;

	public List<UseInfo> getUseInfos() {
		return useInfos;
	}

	public void setUseInfos(List<UseInfo> useInfos) {
		this.useInfos = useInfos;
	}
	
	public static UseRecordResponse valueOf(PageModel<DevicesUsed> pm) {
		UseRecordResponse result = new UseRecordResponse();
		if(pm == null || pm.getDatas() == null || pm.getDatas().isEmpty()) {
			return result;
		}
		List<UseInfo> userInfoList = new ArrayList<UseInfo>();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(DevicesUsed useRecord : pm.getDatas()) {
			if(useRecord == null) {
				continue;
			}
			
			UseInfo useInfo = new UseInfo();
			useInfo.setUseId(useRecord.getDevicesUsedId());
			useInfo.setPartyName(useRecord.getPartyName());
			useInfo.setUseTime(sdf.format(useRecord.getUsedTime()));
			
			useInfo.setStatus(useRecord.getUsedStatus());
			UsedStatusEnum status = UsedStatusEnum.getByCode(useRecord.getUsedStatus());
			useInfo.setStatusName(status.getMessage());
			useInfo.setDevicesName(useRecord.getDevices().getDevicesCode());
			if(useRecord.getSeal() != null) {
				useInfo.setSealName(useRecord.getSeal().getSealName());
			}
			userInfoList.add(useInfo);
			
			CensorStatusEnum censorStatus = CensorStatusEnum.getByCode(useRecord.getCensorStatus());
			if(censorStatus != null) {
				useInfo.setCensorStatus(censorStatus.getCode());
				useInfo.setCensorStatusName(censorStatus.getMessage());
			}
			
		}
		result.setUseInfos(userInfoList);
		return result;
	}
	
}
